<?php

namespace app\api\controller;

use app\common\controller\Api;

class Integralgoods extends Api
{

    protected $noNeedLogin = ['index', 'detail'];

    /**
     * 积分商品首页
     */
    public function index()
    {
        $params = [
            'sort'  => $this->request->param('sort', 'id'),
            'order' => $this->request->param('order', 'ASC'),
            'site'  => $this->request->header('site')
        ];

        $IntegralGoods  = new \app\common\model\IntegralGoods();
        $list           = $IntegralGoods->getList($params);

        $this->success('', ['total' => $list->total(), 'rows' => $list->items()]);
    }

    /**
     * 积分商品详情
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function detail($id)
    {
        $IntegralGoods  = new \app\common\model\IntegralGoods();
        $data           = $IntegralGoods->getDetail($id);

        if (!$data) {
            $this->error('商品不存在');
        }

        if ($data['is_shelves'] != 1) {
            $this->error('商品已下架');
        }

        $this->success('', $data);
    }
}